/*
 * Copyright (c) 2006-2020, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-03-19     Administrator       the first version
 */

#include <rtthread.h>
#include "board.h"
#include "led_app.h"


//#define DRV_DEBUG
#define DBG_TAG         "led.app"
#define DBG_LVL         DBG_WARNING
#include <rtdbg.h>

#define LED_APP_PRIORITY         25
#define LED_APP_STACK_SIZE       512
#define LED_APP_TIMESLICE        5

static rt_thread_t led_app_tid = RT_NULL;


uint8_t red_pin   = 0;
uint8_t blue_pin  = 0;
uint8_t green_pin = 0;

static void led_init(void)
{
    red_pin   = rt_pin_get("PE.1");
    blue_pin  = rt_pin_get("PA.2");
    green_pin = rt_pin_get("PE.4");

    rt_pin_mode(red_pin, PIN_MODE_OUTPUT);
    rt_pin_mode(blue_pin, PIN_MODE_OUTPUT);
    rt_pin_mode(green_pin, PIN_MODE_OUTPUT);

    LOG_D("LED Pin Initation OK!\n");
}

static void led_closed(void)
{
    rt_pin_write(red_pin, PIN_HIGH);
    rt_pin_write(blue_pin, PIN_HIGH);
    rt_pin_write(green_pin, PIN_HIGH);

    LOG_D("Close all LED!\n");
}

static void led_test(uint8_t step)
{
    switch(step)
    {
    case 1:
        led_closed();
        rt_pin_write(red_pin, PIN_LOW);
        LOG_D("Open Red LED!\n");
        break;

    case 2:
        led_closed();
        rt_pin_write(blue_pin, PIN_LOW);
        LOG_D("Open Blue LED!\n");
        break;

    case 3:
        led_closed();
        rt_pin_write(green_pin, PIN_LOW);
        LOG_D("Open Green LED!\n");
        break;

    case 4: // 品红
        led_closed();
        rt_pin_write(red_pin, PIN_LOW);
        rt_pin_write(blue_pin, PIN_LOW);
        LOG_D("Open Fuchsin LED!\n");
        break;

    case 5: // 黄
        led_closed();
        rt_pin_write(red_pin, PIN_LOW);
        rt_pin_write(green_pin, PIN_LOW);
        LOG_D("Open Yellow LED!\n");
        break;

    case 6: // 青
        led_closed();
        rt_pin_write(blue_pin, PIN_LOW);
        rt_pin_write(green_pin, PIN_LOW);
        LOG_D("Open Cyan LED!\n");
        break;

    case 7: // 白
        led_closed();
        rt_pin_write(red_pin, PIN_LOW);
        rt_pin_write(blue_pin, PIN_LOW);
        rt_pin_write(green_pin, PIN_LOW);
        LOG_D("Open White LED!\n");
        break;

    case 8:
        led_closed();
        break;

    default:
        break;
    }
}

static void led_app_entry(void *param)
{
    uint8_t cnt = 0;
    led_init();
    rt_kprintf("red_pin(PE.1):%d\n",red_pin);
    rt_kprintf("blue_pin(PA.2):%d\n",blue_pin);
    rt_kprintf("green_pin(PE.4):%d\n",green_pin);

    while (1)
    {
        cnt++;
        led_test(cnt);
        if (cnt > 8)
        {
            cnt = 0;
        }
        rt_thread_mdelay(1000);
    }
}

void led_app(void)
{
    led_app_tid = rt_thread_create("led_app", led_app_entry, RT_NULL, LED_APP_STACK_SIZE, LED_APP_PRIORITY, LED_APP_TIMESLICE);

    if (led_app_tid != RT_NULL)
    {
        rt_thread_startup(led_app_tid);
    }
}

//MSH_CMD_EXPORT(led_app, led control test);
